package com.boot.mgt.service.impl


import com.boot.config.ParamConfig
import com.boot.core.result.ResultTo
import com.boot.mgt.dao.mysql.TaAccidentImageMapper

import com.boot.mgt.dao.mysql.AccidentFileMapper
import com.boot.mgt.dao.mysql.FrmCodeMapper
import com.boot.mgt.dao.mysql.TaAccidentMapper
import com.boot.mgt.model.TaAccidentImage

import com.boot.mgt.model.AccidentFile
import com.boot.mgt.model.FrmCode
import com.boot.mgt.model.TaAccident

import com.boot.mgt.service.AccidentFileService
import com.github.pagehelper.PageHelper
import com.github.pagehelper.PageInfo
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl
import org.apache.commons.beanutils.BeanUtils


import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

import tk.mybatis.mapper.entity.Example
import java.sql.*

import java.util.*


/**
 *
 * xiaying
 */
@AutoJsonRpcServiceImpl
@Service
class AccidentFileServiceImpl : AccidentFileService {

    @Autowired
    protected lateinit var accidentFileMapper: AccidentFileMapper
    @Autowired
    protected lateinit var accidentMapper: TaAccidentMapper
    @Autowired
    protected lateinit var frmCodeMapper: FrmCodeMapper
    @Autowired
    protected lateinit var accidentImageMapper: TaAccidentImageMapper
    @Autowired
    private lateinit var paramConfig: ParamConfig

    override fun list(page_index: Int, page_size: Int, has_latlon: Int, begin_date: String?, end_date: String?,xzdw:String?,sglx:Int?): ResultTo {

        val list = with(AccidentFile()) {
            PageHelper.startPage<Any>(page_index, page_size)
            val example = Example(AccidentFile::class.java)
            val example2 = Example(AccidentFile::class.java)
            if (begin_date != null && begin_date != "") {
                val dateCriteria = example2.createCriteria()
                dateCriteria.andBetween("sgfssj", begin_date, end_date)
                example.and(dateCriteria)
            }
            val example3 = Example(AccidentFile::class.java)
            val xzqhCriteria = example3.createCriteria()
            //有经纬度数据
            if (has_latlon == 1) {
                xzqhCriteria.andIsNotNull("latitude")
                xzqhCriteria.andIsNotNull("longitude")
                xzqhCriteria.andNotEqualTo("latitude", 0)
                xzqhCriteria.andNotEqualTo("longitude", 0)
            }
            //无经纬度数据
            if (has_latlon == 2) {
                xzqhCriteria.andIsNull("latitude")
                xzqhCriteria.andIsNull("longitude")
            }
            //无效纬度数据
            if (has_latlon == 3) {
                xzqhCriteria.andEqualTo("latitude", 0)
                xzqhCriteria.andEqualTo("longitude", 0)
            }

            xzqhCriteria.andEqualTo("xzqh", xzdw)
            xzqhCriteria.andEqualTo("sglx",sglx);
            example.and(xzqhCriteria)
            example.orderBy("sgfssj").desc()
            accidentFileMapper.selectByExample(example)
        }
        return ResultTo().apply {
            val pageInfo = PageInfo(list)
            this["acd_file_list"] = list
            this["page_index"] = page_index
            this["page_amount"] = pageInfo.pages
            this["page_total"] = pageInfo.total
        }
    }

    /**
     *
     */

    override fun setCoordinates(acdFileId: Int, latitude: String, longitude: String): ResultTo {
        var accidentFile = AccidentFile()
        accidentFile.id = acdFileId
        accidentFile.latitude = latitude.toDouble()
        accidentFile.longitude = longitude.toDouble()
        accidentFileMapper.updateByPrimaryKeySelective(accidentFile)
        return ResultTo()
    }

    override fun addAcdFile(taAcdFile: AccidentFile): ResultTo {
        accidentFileMapper.insert(taAcdFile)
        return ResultTo()
    }


    override fun detail(sgbh: String): ResultTo {
        var accidentFileTemp = AccidentFile()
        accidentFileTemp.sgbh = sgbh
        var accidentFile= accidentFileMapper.selectOne(accidentFileTemp)
        return ResultTo().apply {
            this["accident"] =accidentFile
            val accidentImage = TaAccidentImage()
            accidentImage.accidentId = accidentFileMapper.selectOne(accidentFileTemp).id
            this["image_url_list"] = accidentImageMapper.select(accidentImage)
            this["lmzkCode"] = codeToText("路面状况",accidentFile.lmzk.toString())
            this["tqCode"] = codeToText("天气", accidentFile.tq.toString())
            this["rdyyf1Code"] = codeToText("事故原因分类", accidentFile.rdyyfl.toString())

        }
    }


    /**
     * 根据时间和地点同步同一个案件的经纬度信息
     */
    override fun editGpsTask(taAccidentList: java.util.ArrayList<AccidentFile>) {
        for (accidentFile in taAccidentList) {
            val example = AccidentFile()
            example.sgbh = accidentFile.sgbh
            var accidentFileTemp = accidentFileMapper.selectOne(example)
            accidentFileTemp.latitude = accidentFile.latitude
            accidentFileTemp.longitude = accidentFile.latitude
            accidentFileMapper.updateByPrimaryKey(accidentFileTemp)
        }
    }

    override fun compareAccident(records: ArrayList<HashMap<String, Any>>): ArrayList<AccidentFile> {

        val list = ArrayList<AccidentFile>()
        for (record in records) {
            var accidentFile = AccidentFile()
            BeanUtils.copyProperties(accidentFile, record)
            var sgddTemp = accidentFile.sgdd   //事故发生地址
            if (accidentFile.sgfssj != null || accidentFile.sgfssj.toString().length > 18) {

                var sgfssj = accidentFile.sgfssj.toString()//事故发生时间

                var beforeTime = getMinute(sgfssj, -10)
                var endTime = getMinute(sgfssj, +10)
                val accident = Example(TaAccident::class.java)//案件对象
                val criteriaAccident = accident.createCriteria()
                criteriaAccident.andLike("amapAddress", sgddTemp)
                criteriaAccident.andBetween("occurTime", beforeTime, endTime)
                accident.and(criteriaAccident)

                var taAccident = accidentMapper.selectByExample(accident)//查出案件表中匹配的对象list
                for (taAccidentVo in taAccident) {
                    if (taAccidentVo.amapLatitude != null && taAccidentVo.amapLongitude != null) {
                        accidentFile.latitude = taAccidentVo.amapLatitude
                        accidentFile.longitude = taAccidentVo.amapLongitude
                        list.add(accidentFile)
                    }
                }


            }
        }
        return list
    }

    companion object {
        private val log: Logger = LoggerFactory.getLogger(this::class.java)
    }

    fun getMinute(times: String, value: Int): Timestamp {
        var timeString = times

        val timeTemp = Timestamp.valueOf(timeString)

        val cal = Calendar.getInstance()
        cal.time = timeTemp
        cal.add(Calendar.MINUTE, value)
        return Timestamp(cal.timeInMillis)


    }


    /**
     * 天气，路面状况等根据key值转换成对应的数据
     */
    private fun codeToText(type: String, dmz: String): String {
        var frmCode=FrmCode()
        frmCode.dmz=dmz
         if(type.equals("天气"))
         {
             frmCode.dmlb="0111"
             frmCode.xtlb="03"
         }
        if(type.equals("路面状况"))
        {
            frmCode.dmlb="0117"
            frmCode.xtlb="03"
        }
        if(type.equals("事故原因分类"))
        {
            frmCode.dmlb="0115"
            frmCode.xtlb="03"
        }
        var frmCodeTemp=frmCodeMapper.selectOne(frmCode)
        var  codeString = frmCodeTemp.dmsm1.toString()

        return codeString
    }


}

